package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.entity.product.ProductSpec;
import com.atguigu.spzx.model.vo.product.ProductItemVo;
import com.atguigu.spzx.model.vo.product.ProductSkuDto;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;


    @Override
    public List<ProductSku> findProductSkuBySale() {
        List<ProductSku> productSkuList= productSkuMapper.selectProductSkuBySale();
        return productSkuList;
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        PageInfo<ProductSku> pageInfo=new PageInfo<>(productSkuList);
        return pageInfo;
    }

    @Override
    public ProductItemVo item(Long skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        //查询商品sku信息
        ProductSku productSku= productSkuMapper.selectProductSkuById(skuId);
        Long productId = productSku.getProductId();
        //查询商品信息
        Product product= productMapper.selectProductById(productId);
        //查询商品轮播图列表   在product里,我们需要把里面数据拆分,为String数组形式
        String sliderUrls = product.getSliderUrls();
        //查询商品详情图片列表
        ProductDetails productDetails = productDetailsMapper.selectProductDetailsByProductId(productId);
        //查询商品规格信息
        String specValue = product.getSpecValue();

        //查询商品规格对应商品skuId信息  需要产品的sku集合.
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(productId);

        //封装数据
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        String[] split = sliderUrls.split(",");
        productItemVo.setSliderUrlList(Arrays.asList(split));
        String[] images = productDetails.getImageUrls().split(",");
        productItemVo.setDetailsImageUrlList(Arrays.asList(images));
        productItemVo.setSpecValueList(JSON.parseArray(specValue));
        Map<String,Object> map =new HashMap<>();
        for (ProductSku item : productSkuList) {
            map.put(item.getSkuSpec(),item.getId());
        }
        productItemVo.setSkuSpecValueMap(map);


        return productItemVo;
    }

    @Override
    public Boolean updateStock(Long skuId, Integer skuNum) {
        int row= productMapper.updateStock(skuId,skuNum);
        return row > 0;
    }


}
